﻿using System;
using IListNode;

namespace _404_sum_of_left_leaves
{
    class Program
    {
        static void Main (string[] args)
        {
            TreeNode root = new TreeNode (2);
            root.left = new TreeNode (1);
            root.right = new TreeNode (4);
            root.right.left = new TreeNode (3);
            root.right.left.left = new TreeNode (5);

            Console.WriteLine (new Solution ().SumOfLeftLeaves (root));
        }
    }

    public class Solution
    {
        public int SumOfLeftLeaves (TreeNode root)
        {
            if (root == null) return 0;
            int sum = 0;

            if (root.left != null && root.left.left == null && root.left.right == null)
            {
                sum = root.left.val;
            }

            return sum + SumOfLeftLeaves(root.left) + SumOfLeftLeaves(root.right);
        }
    }

   
}
